<?php
/**
 * Created by singwa
 * User: singwa
 * motto: 现在的努力是为了小时候吹过的牛逼！
 * Time: 07:49
 */

namespace app\api\exception;

use think\exception\Handle;
use think\Response;
use Throwable;

class ExceptionHandler extends Handle
{

    public $httpStatus = 500;

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {

//        echo "<pre>";
//        var_dump($e);
//        halt();

        if ($e instanceof BaseException) {
            return show($e->getCode(), $e->msg);
        } else {
            if ($e instanceof \think\Exception) {
                 return show($e->getCode(), $e->getMessage());
            }
            if ($e instanceof \think\exception\HttpResponseException) {
                return parent::render($request, $e);
            }

            if (method_exists($e, "getStatusCode")) {
                $httpStatus = $e->getStatusCode();
            } else {
                $httpStatus = $this->httpStatus;
            }

            // 添加自定义异常处理机制
            return show(config("status.error"), $e->getMessage(), [], $httpStatus);
        }
    }
}